package com.example.saifly.inPageRecord.BodyHistoryData;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.common.Retrofit.RetrofitHelper;
import com.example.saifly.R;
import com.example.saifly.inPageRecord.RecordModel.BodyData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

public class BodyHistoryBloodPressureActivity extends Activity {
    @Override
    protected int getContentLayoutId()
    {
        return R.layout.activity_bloodpressure_history;
    }
    private ImageView ivReturn;
    private int bloodPressureHighData[]=new int[5];
    private int bloodPressureLowData[]=new int[5];
    private String inputTime[]=new String[5];
    private List<BodyData> BloodPressureList;

    private TextView tvInputTime1;
    private TextView tvInputTime2;
    private TextView tvInputTime3;
    private TextView tvInputTime4;
    private TextView tvInputTime5;
    private TextView tvBloodPressureHighData1;
    private TextView tvBloodPressureHighData2;
    private TextView tvBloodPressureHighData3;
    private TextView tvBloodPressureHighData4;
    private TextView tvBloodPressureHighData5;
    private TextView tvBloodPressureLowData1;
    private TextView tvBloodPressureLowData2;
    private TextView tvBloodPressureLowData3;
    private TextView tvBloodPressureLowData4;
    private TextView tvBloodPressureLowData5;

    private SharedPreferences sharedPreferences= MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);

    @Override
    protected void init()
    {
        super.init();

        ivReturn = findViewById(R.id.bloodPressure_return);

        BloodPressureList=new ArrayList<>();
        tvInputTime1=findViewById(R.id.bloodPressureTime1);
        tvInputTime2=findViewById(R.id.bloodPressureTime2);
        tvInputTime3=findViewById(R.id.bloodPressureTime3);
        tvInputTime4=findViewById(R.id.bloodPressureTime4);
        tvInputTime5=findViewById(R.id.bloodPressureTime5);
        tvBloodPressureHighData1=findViewById(R.id.bloodPressureHigh1);
        tvBloodPressureHighData2=findViewById(R.id.bloodPressureHigh2);
        tvBloodPressureHighData3=findViewById(R.id.bloodPressureHigh3);
        tvBloodPressureHighData4=findViewById(R.id.bloodPressureHigh4);
        tvBloodPressureHighData5=findViewById(R.id.bloodPressureHigh5);
        tvBloodPressureLowData1=findViewById(R.id.bloodPressureLow1);
        tvBloodPressureLowData2=findViewById(R.id.bloodPressureLow2);
        tvBloodPressureLowData3=findViewById(R.id.bloodPressureLow3);
        tvBloodPressureLowData4=findViewById(R.id.bloodPressureLow4);
        tvBloodPressureLowData5=findViewById(R.id.bloodPressureLow5);

        for(int i=0;i<5;i++)
        {
            bloodPressureHighData[i]=0;
           // Log.d("啦啦啦啦", "高压: "+bloodPressureHighData[i]);
            bloodPressureLowData[i]=0;
           // Log.d("啦啦啦啦", "低压: "+bloodPressureLowData[i]);
            inputTime[i]="未录入";
            //Log.d("啦啦啦啦", "输入时间: "+inputTime[i]);
        }

        new Thread(new Runnable() {
            @Override
            public void run()
            {
                bloodPressureHistory(sharedPreferences.getString("uid","default"));
            }
        }).start();

        doJump();

    }

    private interface getTopBodyData {
        @Headers({"Content-Type:application/json"})
        @POST("/estimate/getTopBodyData")
        Call<ResponseBody> bloodPressureHistory(@Body JSONObject jsonObject);
    }

    private void bloodPressureHistory(String phoneNumber) {
        RetrofitHelper retrofitHelper = new RetrofitHelper();
        Retrofit retrofit = retrofitHelper.getRetrofit();

        getTopBodyData apiBloodPressureHistory = retrofit.create(getTopBodyData.class);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("patientPhone",phoneNumber);
//        Log.d("啦啦啦啦", "logData: "+jsonObject);

        Call<ResponseBody> call = apiBloodPressureHistory.bloodPressureHistory(jsonObject);
        final Response<ResponseBody> response;
        try {
            response = call.execute();
//            Log.d("啦啦啦啦", "logData: "+jsonResult);
            String jsonResult=response.body().string();
            Log.d("血压", "logData: "+jsonResult);
            JSONObject result = JSON.parseObject(jsonResult);
            Log.d("啦啦啦啦", "logData: "+result);

            if (result.getInteger("code") == 1) {
                //获取的code为1 说明请求成功
                List<BodyData> bodyData = JSON.parseArray((String) result.get("result"),BodyData.class);

                for(int i=0;i<bodyData.size();i++)
                {
                    Date tempTime=bodyData.get(i).getDate();
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    inputTime[i]=dateFormat.format(tempTime);
                    bloodPressureHighData[i]=bodyData.get(i).getHeightBloodPressure();
                    bloodPressureLowData[i]=bodyData.get(i).getLowBloodPressure();
                }
                if(bloodPressureHighData[0]==0&&bloodPressureLowData[0]==0)  inputTime[0]="未录入";
                showBloodPressure();
            } else {
                //请求失败
                runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Toast toast=Toast.makeText(BodyHistoryBloodPressureActivity.this, "获取血压数据失败，请重新尝试", Toast.LENGTH_LONG);
                        showMyToast(toast,10*500);// 设置显示时间  一次10ms,显示500次，即显示5s
                    }
                });
            }
        } catch (Exception e) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(BodyHistoryBloodPressureActivity.this, "服务器在开小差儿", Toast.LENGTH_SHORT).show();
                }
            });
            e.printStackTrace();
        }
    }
    //Toast 自定义显示时长
    public void showMyToast(final Toast toast, final int cnt) {
        final Timer timer =new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toast.show();
            }
        },0,3000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toast.cancel();
                timer.cancel();
            }
        }, cnt );
    }

    public void showBloodPressure()
    {
        tvInputTime1.setText(inputTime[0]);
        tvInputTime2.setText(inputTime[1]);
        tvInputTime3.setText(inputTime[2]);
        tvInputTime4.setText(inputTime[3]);
        tvInputTime5.setText(inputTime[4]);
        tvBloodPressureHighData1.setText(String.valueOf(bloodPressureHighData[0])+"mmHg");
        tvBloodPressureHighData2.setText(String.valueOf(bloodPressureHighData[1])+"mmHg");
        tvBloodPressureHighData3.setText(String.valueOf(bloodPressureHighData[2])+"mmHg");
        tvBloodPressureHighData4.setText(String.valueOf(bloodPressureHighData[3])+"mmHg");
        tvBloodPressureHighData5.setText(String.valueOf(bloodPressureHighData[4])+"mmHg");
        tvBloodPressureLowData1.setText(String.valueOf(bloodPressureLowData[0])+"mmHg");
        tvBloodPressureLowData2.setText(String.valueOf(bloodPressureLowData[1])+"mmHg");
        tvBloodPressureLowData3.setText(String.valueOf(bloodPressureLowData[2])+"mmHg");
        tvBloodPressureLowData4.setText(String.valueOf(bloodPressureLowData[3])+"mmHg");
        tvBloodPressureLowData5.setText(String.valueOf(bloodPressureLowData[4])+"mmHg");
    }

    private void doJump()
    {
        ivReturn.setOnClickListener(view -> finish());
    }
}
